home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 50
/
Aminet 50 (2002)(GTI - Schatztruhe)[!][Aug 2002].iso
/
Aminet
/
util
/
cli
/
netid.lha
/
netid.c
< prev
next >
Wrap
C/C++ Source or Header
|
2002-05-17
|
3KB
|
123 lines
/*
NetID 0.2, Calculates NetID and the number of hosts && nets
for a given IP-number and Subnet-mask.
Copyright © 2002 Kalle Räisänen.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Syntax: NetID ip-number subnet-mask
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int netid[4], ip[4], sub[4];
int countbits(int num)
{
int ret = 0, i;
for(i = 0; i < 8; i++)
if(num & (1<<i))
ret++;
return ret;
}
int toip(char *s, int *ip)
{
int i = 0, j = 0;
while(s[j])
{
if(s[j] == '.')
{
if(ip[i] > 255) return 0;
i++;
}
else if((s[j] >= '0') && (s[j] <= '9'))
{
ip[i] *= 10;
ip[i] += s[j] - '0';
}
else
return 0;
j++;
}
if(ip[i] > 255) return 0;
return 1;
}
void getnetid(int *netid, int *ip, int *sub)
{
int i = 0;
for(; i < 4; i++)
netid[i] = sub[i] & ip[i];
}
unsigned int nets = 0, hosts = 0;
void getnets(int *subm, int ipcl)
{
int i = 1, nbit = 0, hbit = 0;
if(ipcl >= 192)
i = 3;
else if(ipcl >= 128)
i = 2;
for(;i < 4; i++)
{
nbit += countbits(subm[i]);
hbit += countbits(~subm[i]);
}
nets = abs(pow(2, nbit)-2);
hosts = abs(pow(2, hbit)-2);
}
int main(int argc, char *argv[])
{
printf("\nNetID 0.2, Copyright (c) 2002 Kalle Raisanen.\n\n");
if(argc != 3)
{
fprintf(stderr, "\tUsage: %s ip sub\n\n", argv[0]);
fprintf(stderr, "\tWhere ip is the IP-adress and sub the Subnet-mask.\n");
return 0;
}
if(!toip(argv[1], ip))
{
fprintf(stderr, "%s isn't a valid IP-adress!\n", argv[1]);
return 0;
}
if(!toip(argv[2], sub))
{
fprintf(stderr, "%s isn't a valid IP-adress!\n", argv[2]);
return 0;
}
getnetid(netid, ip, sub);
getnets(sub, ip[0]);
printf(" %d.%d.%d.%d/%d:\n", ip[0], ip[1], ip[2], ip[3],
countbits(sub[0]) + countbits(sub[1]) + countbits(sub[2]) + countbits(sub[3]));
printf("\tNet ID: %d.%d.%d.%d\n", netid[0], netid[1], netid[2], netid[3]);
printf("\t%d nets, %d hosts.\n\n", nets, hosts);
return 0;
}